window.sentlog = window.sentlog || {referrer:false};
window.site_name = window.site_name || '58';
window.page_type = window.page_type || 'NA';
window.post_count = window.post_count || -1;
window.tgreg = window.tgreg || -1;
window._trackURL = window._trackURL || 'NA';
window._trackParams = window._trackParams || 'NA';
var referrer = encodeURIComponent(document.referrer);


(function(){
$("[tongji_tag]").bind('click', function() {var tongji_tag = $(this).attr("tongji_tag");clickLog('from='+tongji_tag);});
})();


(function(){
var pageT = _trackURL || 'NA';
if(pageT.indexOf("\'pagetype\':\'home\'") !=-1){
$("[href]").bind('click', function(e) {var href = $(this).attr("href");var tagY = $(this).offset().top;var tagX = $(this).offset().left;var mouseX = e.pageX;var mouseY = e.pageY;clickLog('from=heatmap:'+href+'&tagX='+tagX+'&tagY='+tagY+'&mouseX='+mouseX+'&mouseY='+mouseY);});}
})();




if (!sentlog.referrer) {
	_trackParams = format_trackParams(_trackParams);
	_trackParams = encodeURIComponent(_trackParams);
	var logmsg = "tag=pvstatall&referrer="+referrer+"&type="+page_type+"&post_count="+post_count + "&_trackParams=" + _trackParams;
	if (page_type=='detail') {
		try{
			logmsg = logmsg + '&userid='+____json4fe.userid;
			if ('phone' in ____json4fe) logmsg = logmsg + '&huzhuan='+____json4fe.phone;
			if (tgreg > 0) logmsg = logmsg + '&tgreg='+tgreg;
		}catch(e){}
	}
	try{
		if ('version' in ____json4fe) logmsg = logmsg + '&version='+____json4fe.version;
		if ('smsc' in ____json4fe) logmsg = logmsg + '&smsc='+____json4fe.smsc;
		if ('start' in ____json4fe) logmsg += '&loadtime='+((new Date()).getTime()-____json4fe.start);
		if ('supplycount' in ____json4fe){
			for(var attr in ____json4fe.supplycount){
				logmsg = logmsg + '&supplycount.'+attr+'='+____json4fe.supplycount[attr];
			}
		}
	}catch(e){}
        try{logmsg = logmsg + '&window_size='+document.documentElement.clientWidth+'x'+document.documentElement.clientHeight;}catch(e){}
	if (_trackURL != 'NA') logmsg = logmsg + '&trackURL='+_trackURL;
	try{
		var m_ga_utma = getcookie('__utma');
		if(m_ga_utma && m_ga_utma != ''){
			logmsg = logmsg + '&_ga_utma=' + m_ga_utma;
		}
		
		
		var referrerURL = window.location.href;
		var utm_source = 'NA';
        var utm_campaign = 'NA';

		if(referrerURL.indexOf("utm_source=") != -1)
		{
			var index_of_source = referrerURL.indexOf("utm_source=");
			var utm_source_tmp = referrerURL.substr(index_of_source);
			var index_of_and = utm_source_tmp.indexOf("&");
			
			if(index_of_and == -1)
			{
				var key_val = utm_source_tmp.split("=");
				utm_source = key_val[1];
			}
			else
			{
				var utm_source_tmp2 = utm_source_tmp.substr(0, index_of_and);
				var key_val = utm_source_tmp2.split("=");
				utm_source = key_val[1];
			}
			setcookie('utm_source',utm_source,1);

            if(referrerURL.indexOf("utm_campaign=") != -1)
            {
                var index_of_campaign = referrerURL.indexOf("utm_campaign=");
                var utm_campaign_tmp = referrerURL.substr(index_of_campaign);
                var index_of_and = utm_campaign_tmp.indexOf("&");

                if(index_of_and == -1)
                {
                    var key_val = utm_campaign_tmp.split("=");
                    utm_campaign = key_val[1];
                }
                else
                {
                    var utm_campaign_tmp2 = utm_campaign_tmp.substr(0, index_of_and);
                    var key_val = utm_campaign_tmp2.split("=");
                    utm_campaign = key_val[1];
                }
            }
            setcookie('utm_campaign',utm_campaign,1);

		}
		else
		{
			var Cookie_utm_source = getcookie('utm_source');
            var Cookie_utm_campaign = getcookie('utm_campaign');
			if(Cookie_utm_source == '' || Cookie_utm_source == 'NA' )
			{
				setcookie('utm_source','NA',1);
			}
			else
			{
				utm_source = Cookie_utm_source;
			}
            if(Cookie_utm_campaign == '' || Cookie_utm_campaign == 'NA' )
            {
                setcookie('utm_campaign','NA',1);
            }
            else
            {
                utm_campaign = Cookie_utm_campaign;
            }


		}

        if (_trackURL != 'NA')
        {
            var tmpArr = logmsg.split("&trackURL={");
            logmsg = tmpArr[0] + '&trackURL={' + "\'utm_source\':\'" +utm_source +"\'," +"\'utm_campaign\':\'" +utm_campaign +"\'," + tmpArr[1];
        }
 
		
	}catch(e){}
	ajaxlog_new(logmsg);
	try {
		var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");
		var sNode = document.createElement("script");
		sNode.type = "text/javascript";
		sNode.src = cnzz_protocol + "w.cnzz.com/c.php?id=30017898";
		document.getElementsByTagName("head")[0].appendChild(sNode);
	} catch (e_cnzz) {}
	sentlog.referrer = true;

}
function setcookie(name,val,days){
	var exp = new Date();
	exp.setTime(exp.getTime() + days*86400000);
	document.cookie = name + '=' + escape(val) + '; expires=' + exp.toGMTString();
}
function getcookie(name){
	var arr, reg=new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if(arr = document.cookie.match(reg)){
		return unescape(arr[2]);
	}
	return '';
}
function ajaxsend(url){
	var img = new Image();
	img.src = url;
}
function ajaxlog(log){
	ajaxsend(document.location.protocol+"//track.58.com/empty.js.gif?site_name="+site_name+"&"+log+"&rand="+Math.random());
    ajaxsend(document.location.protocol+"//tracklog.58.com/pc/empty.js.gif?site_name="+site_name+"&"+log+"&rand="+Math.random());
}

function ajaxlog_new(log){
	var rand_log_id = Math.random();
	ajaxsend(document.location.protocol+"//tracklog.58.com/pc/empty.js.gif?fromid=referrer4&site_name="+site_name+"&"+log+"&rand_id="+rand_log_id);
	//ajaxsend(document.location.protocol+"//track.58.com/empty.js.gif?fromid=referrer4&site_name="+site_name+"&"+log+"&rand_id="+rand_log_id);
}

function ajaxlog2(log){
	ajaxlog(log);
}

function ajaxlogr(log){
	ajaxsend(document.location.protocol+"//track.58.com/adsys/listclicklog?site_name="+site_name+"&"+log+"&trackURL="+_trackURL+"&rand="+Math.random());
	ajaxsend(document.location.protocol+"//tracklog.58.com/pc/click/listclicklog?site_name="+site_name+"&"+log+"&trackURL="+_trackURL+"&rand="+Math.random());
}

function format_trackParams(paramContent) {
	if(paramContent == "NA") {
		return paramContent;
	}
	try{
		var _trackArrParams = [];
		for(var i = 0, ln = paramContent.length; i < ln; i++){
			_trackArrParams.push(paramContent[i].I + ":" + paramContent[i].V.replace(/\|/g,"*"));
		}
		return _trackArrParams.join("@@");
	}catch(e){
		return "error";
	}
}

function clickLog(log){
	ajaxsend(document.location.protocol+"//tracklog.58.com/pc/click/empty.js.gif?site_name="+site_name+"&tag=pvsiters&"+log+"&trackURL="+_trackURL+"&rand="+Math.random());
}

(function(){
    if((!!window.jQuery) && (!!window.__gsxw_conf)){
        var postInput = $("<input />").attr({
            type:"hidden",
            name:__gsxw_conf.name,
            value:""
        })
        $("#"+__gsxw_conf.form).append(postInput);
        var queryStr = "#"+__gsxw_conf.inputs.join(",#"),
            valueArr = [],
            _posX,_posY;
        $("body").bind("mousedown",function(e){
            var event = e || window.event;
            _posX = event.pageX;
            _posY = event.pageY;
        })
        $(queryStr).bind("click",function(){
            var dataArr = [_posX,_posY,new Date().getTime(),$(this).attr("id"),"focus","txt"];
            valueArr.push(dataArr.join(","));
            postInput.val(valueArr.join(";"));
        })
        $(queryStr).bind("blur",function(){
            var dataArr = [_posX,_posY,new Date().getTime(),$(this).attr("id"),"blur","txt"];
            valueArr.push(dataArr.join(","));
            postInput.val(valueArr.join(";"));
        })
        $("#"+__gsxw_conf.btns).bind("mouseup",function(e){
            var dataArr = [_posX,_posY,new Date().getTime(),$(this).attr("id"),"click","button"];
            valueArr.push(dataArr.join(","));
            postInput.val(valueArr.join(";"));
        })
    }
})();
